import 'package:flutter/material.dart';

//引入页面模块
import '../views/index/index.dart';
import '../views/login/login.dart';
import '../views/box/box.dart';
import '../views/error/error.dart';
import '../views/test/test.dart';
/**具体业务页面模块**/
import '../views/home/home.dart';
import '../views/search/search.dart';
import '../views/publish/publish.dart';
import '../views/message/message.dart';
import '../views/user/user.dart';
import '../views/favorite/favorite.dart';

//https://book.flutterchina.club/chapter2/flutter_router.html
/*
当调用Navigator.pushNamed(...)打开命名路由时，如果指定的路由名在路由表中已注册，
则会调用路由表中的builder函数来生成路由组件；如果路由表中没有注册，才会调用onGenerateRoute来生成路由。
*/

//命名路由配置
final routes = {
  '/': (context) => Index(),
  "/login": (context) => LoginPage(),
  "/box": (context) => BoxPage(),
  "/home": (context) => HomePage(),
  "/favorite": (context, {arguments}) => FavoritePage(arguments: arguments),
  "/error": (context) => ErrorPage(),
  "/test": (context) => TestPage(),
};

//onGenerateRoute路由管理跳转和使用拦截器
Route<dynamic> onGenerateRoute(RouteSettings settings) {
  print('onGenerateRoute');
  print(settings);
  // 统一处理
  final String name = settings.name;
  final Function pageContentBuilder = routes[name];
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      return MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
    } else {
      //return AnimationRoute(pageContentBuilder());
      return MaterialPageRoute(
          builder: (context) => pageContentBuilder(context));

    }
  }
}

// 未知页面，404
Route<dynamic> onUnknownRoute(RouteSettings setting) {
  String name = setting.name;
  print("未匹配到路由:$name");
  return new MaterialPageRoute(builder: (context) {
    return new ErrorPage();
  });
}

class AnimationRoute extends PageRouteBuilder {
  final Widget widget;

  AnimationRoute(this.widget)
      : super(
            transitionDuration: const Duration(milliseconds: 500), //设置动画时长500毫秒
            pageBuilder: (BuildContext context, Animation<double> animation1,
                Animation<double> animation2) {
              return widget;
            },
            transitionsBuilder: (BuildContext context,
                Animation<double> animation1,
                Animation<double> animation2,
                Widget child) {
              //左右滑动
              return SlideTransition(
                position: Tween<Offset>(
                        begin: Offset(-1.0, 0.0), end: Offset(0.0, 0.0))
                    .animate(CurvedAnimation(
                        parent: animation1, curve: Curves.fastOutSlowIn)),
                child: child,
              );
            });
}
